package synchronizedTest;


/**
 * 1、修饰方法  ：
 *          1.1 静态方法 ，锁Class对象
 *          1.2 普通方法 ，肯定是对象掉方法，锁当前对象。
 * 2、修饰代码块
 *          传什么，锁什么对象
 * */
public class synchronizedWay {

    int val = 0;
    public static void main(String[] args) {
         synchronizedWay synchronizedWay = new synchronizedWay();
         synchronizedWay.threadTest();
    }

    public void threadTest(){
        Thread t1 = new Thread(() -> count() );
        Thread t2 = new Thread(() -> count() );
        Thread t3 = new Thread(() -> count() );
        Thread t4 = new Thread(() -> count() );
        Thread t5 = new Thread(() -> count() );
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();

        try {
            t1.join();
            t2.join();
            t3.join();
            t4.join();
            t5.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(val);
    }

     synchronized  void count() {
        for (int j = 0; j < 10000; j++) {
            val++;
        }
    }
}
